﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using EasySL.Core;
using EasySL.Entity;
using System.Collections.ObjectModel;
using System.Windows.Browser;
using System.Windows.Data;
using System.Reflection;
using EasySL.Controls;

namespace EasySL.UI
{
    public partial class Page : UserControl
    {      
        public Page()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(Page_Loaded);
            this.df.EditEnding += new EventHandler<DataFormEditEndingEventArgs>(df_EditEnding);
            this.df.DeletingItem += new EventHandler<System.ComponentModel.CancelEventArgs>(df_DeletingItem);
            this.pager.PageIndexChange += this.pager_PageIndexChanged;
        }

        void pager_PageIndexChanged()
        {
            //EasySL.Controls.Window.Confirm("aaaa", () =>
            //    {
            //        var a = 1;
            //    });
            
            Task task = new Task();

            task.Begin += (t) =>
            {
                t.MethodName = "GetProducts";
                t.SetParameter("pageIndex", pager.PageIndex);
                t.SetParameter("pageSize", pager.PageSize);
                t.ReturnType = typeof(List<Product>);

                this.loading.Show("Get products...................");
            };

            task.End += res =>
            {
                this.products = new ObservableCollection<Product>();

                int maxNumber = pager.PageSize * pager.PageIndex;

                this.products.CollectionChanged += (s, e) =>
                {
                    if (e.NewItems != null)
                    {
                        var product = e.NewItems[0] as Product;
                        product.Number = (maxNumber++) + 1;
                    }
                };

                foreach(var product in res.Data as List<Product>)
                {
                    this.products.Add(product);
                }

                this.Dispatcher.BeginInvoke(() =>
                {
                    this.dg.ItemsSource = this.df.ItemsSource = this.products;
                    this.loading.Hide();
                });
            };
            task.Start();
        }

        void Page_Loaded(object sender, RoutedEventArgs re)
        {
            TaskList taskList = new TaskList();
            
            Task task = new Task();
            task.Begin += (t) =>
            {
                t.MethodName = "Connect";
                t.ReturnType = typeof(bool);
            };
            task.End += res =>
            {
                var isConnect = res.Data;
            };

            Task task2 = new Task();
            task2.Begin += (t) =>
            {
                t.MethodName = "GetProductCount";
                t.ReturnType = typeof(int);
            };
            task2.End += res =>
            {
                var itemCount = (int)res.Data;
                this.Dispatcher.BeginInvoke(() =>
               {
                   this.pager.ItemCount = itemCount;
                   this.pager.PageIndex = 0;
               });
            };

            taskList.Add(task);
            taskList.Add(task2);
            taskList.Start();      
        }

        void df_EditEnding(object sender, DataFormEditEndingEventArgs e)
        {
            if (e.EditAction == DataFormEditAction.Commit)
            {
                var product = this.df.CurrentItem as Product;

                Task task = new Task();

                task.Begin += (t) =>
                {
                    t.MethodName = "SaveOrAddProduct";
                    t.SetParameter("product", product);
                    t.ReturnType = typeof(bool);

                    this.loading.Show("Add/Edit product...................");
                };

                task.End += res =>
               {
                   this.Dispatcher.BeginInvoke(() =>
                  {
                      this.loading.Hide();
                  });
               };

                task.Start();
            }
        }

        void df_DeletingItem(object sender, System.ComponentModel.CancelEventArgs e)
        {
          
                var product = this.df.CurrentItem as Product;

                Task task = new Task();

                task.Begin += (t) =>
                {
                    t.MethodName = "DeleteProduct";
                    t.SetParameter("id", product.Id);

                    this.loading.Show("Delete product...................");
                };

                task.End += res =>
                {
                    this.Dispatcher.BeginInvoke(() =>
                    {
                        this.loading.Hide();
                    });
                };

                task.Start(); 
        }

        private ObservableCollection<Product> products;
    }
}
